package activitis;

import interfaces.PostSeleccion;

import java.util.ArrayList;
import java.util.List;

import utils.BaseAsync;
import utils.Constantes;
import utils.SelectorCaracteristicas;

import com.example.rapichofer.R;
import com.google.gson.Gson;

import controladores.ControladorCaracteristicas;

import entidades.Caracteristica;
import entidades.Movil;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class RegistroMovilActivity extends Activity implements PostSeleccion
{	
	public static final int REGISTRO_MOVIL = 445;
	
	private Movil movil;
	
	private EditText campoMarca;
	private EditText campoModelo;
	private EditText campoPatente;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registro_movil);
		
		campoMarca = (EditText) findViewById(R.id.campoMarcaRegistroMovil);
		campoModelo = (EditText) findViewById(R.id.campoModeloRegistroMovil);
		campoPatente = (EditText) findViewById(R.id.campoPatenteRegistroMovil);
		
		movil = new Movil();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.registro_movil, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void aceptar (View view)
	{
		construir();
		
		if (!checkString(movil.getMarca()))
		{
			Toast.makeText(this, "Marca invalida.", Toast.LENGTH_SHORT).show();
			return;
		}
		
		if (!checkString(movil.getModelo()))
		{
			Toast.makeText(this, "Modelo invalido.", Toast.LENGTH_SHORT).show();
			return;
		}
		
		if (!checkString(movil.getPatente()))
		{
			Toast.makeText(this, "Patente invalida.", Toast.LENGTH_SHORT).show();
			return;
		}
		
		if (!checkPatente())
		{
			Toast.makeText(this, "Patente invalida.", Toast.LENGTH_SHORT).show();
			return;
		}
		
		new SelectorCaracteristicas(RegistroMovilActivity.this, this).execute();
	}
	
	private void construir ()
	{
		movil.setMarca(campoMarca.getText().toString());
		movil.setModelo(campoModelo.getText().toString());
		movil.setPatente(campoPatente.getText().toString());
	}
	
	private boolean checkString (String check)
	{
		return check != null && !check.equals("");
	}
	
	private boolean checkPatente ()
	{
		String patente = movil.getPatente();
		
		if (patente.length() != 7)
			return false;
		
		String pat[] = patente.split("-");
		
		if (pat.length != 2)
			return false;
		
		try
		{
			Integer.parseInt(pat[1]);
			return true;
		}
		catch (NumberFormatException ex)
		{
			return false;
		}
	}

	@Override
	public void postSeleccion(List<Caracteristica> seleccion)
	{
		movil.setCaracteristicas(seleccion);		
		
		Intent intent = new Intent();
        intent.putExtra(Constantes.RegistroUsuario.MOVIL, new Gson().toJson(movil));
        setResult(Activity.RESULT_OK, intent);
        finish();
	}
}
